package com.songtao.rabbitmq.routing;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.songtao.rabbitmq.config.RabbitConfig;
import com.songtao.rabbitmq.util.RabbitConnectionUtils;

public class Recv2 {
    
    private static final String QUEUE_NAME = "test_queue_name_2";
    
    public static void main(String[] args) throws IOException, TimeoutException {
        //接收项目
        
        Connection connection = RabbitConnectionUtils.getConnection();
        
        final Channel channel = connection.createChannel();
        
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        
        channel.queueBind(QUEUE_NAME, RabbitConfig.EXCHANGE_SECOND, "error");
        
        Consumer consumer = new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                    throws IOException {
                
                System.out.println("recv2  receive "+new String(body));
                
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally {
                    System.out.println("recv2  receive done");
                  //手动回调给他们
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
            
        };
        
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, autoAck, consumer);
        
    }
}
